package com.matriq.cloudmanager;

import java.util.List;

import com.matriq.cloudmanager.R;
import com.matriq.cloudmanager.entity.CourseInfo;
import com.matriq.cloudmanager.entity.db.DownloadCourseInfo;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView.LayoutParams;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import net.lgpage.activity.BaseActivity;
import net.lgpage.db.DBUtil;
import net.lgpage.http.ExceptionAsyncTask;
import net.lgpage.util.Cleaner;

public class DownloadMngActivity extends BaseActivity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ac_download_mng);
		setViews();
		setEvents();
		loadData();
	}

	android.support.v4.widget.SwipeRefreshLayout srl;
	Button bt_edit;
	android.support.v7.widget.RecyclerView rcy_download_mng;
	TextView tv_title;
	ImageButton ll_back;

	private void setViews() {
		srl = (android.support.v4.widget.SwipeRefreshLayout) this.findViewById(R.id.srl);
		bt_edit = (Button) this.findViewById(R.id.bt_edit);
		rcy_download_mng = (android.support.v7.widget.RecyclerView) this.findViewById(R.id.rcy_download_mng);
		rcy_adp = new RcyAdp();
		rcy_download_mng.setLayoutManager(new LinearLayoutManager(this));
		rcy_download_mng.setAdapter(rcy_adp);
		tv_title = (TextView) this.findViewById(R.id.tv_title);
		ll_back = (ImageButton) this.findViewById(R.id.ll_back);
	}

	private void setEvents() {
		bt_edit.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {

			}
		});
	}

	Object obj;

	public void setData(Object obj) {
		this.obj = obj;
		tv_title.setText(null);
	}

	private class RcyViewHolder extends RecyclerView.ViewHolder {
		LinearLayout ll;
		ImageView iv;
		ProgressBar pb;
		TextView tv[] = new TextView[4];
		Button bt_del;
		LinearLayout ll_progress;
		Button bt;

		public RcyViewHolder(View view) {
			super(view);
		}
	}

	RcyAdp rcy_adp;

	class RcyAdp extends RecyclerView.Adapter<RcyViewHolder> {

		LayoutParams lp;

		@Override
		public int getItemCount() {
			if (rcylist == null)
				return 0;
			return rcylist.size();
		}

		public RcyAdp() {
			super();
			lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		}

		@Override
		public void onBindViewHolder(RcyViewHolder h, final int position) {
			final DownloadCourseInfo obj = list.get(position);
			loadImage(null, h.iv);
			h.tv[0].setText(null);
			h.tv[1].setText(null);
			h.tv[2].setText(null);
			h.tv[3].setText(null);
			h.bt_del.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {

				}
			});
			h.bt.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {

				}
			});
			h.itemView.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {

				}
			});
		}

		@SuppressLint("InflateParams")
		@Override
		public RcyViewHolder onCreateViewHolder(ViewGroup vgp, final int position) {
			View cv = getLayoutInflater().inflate(R.layout.rcy_download_mng, null);
			cv.setLayoutParams(lp);
			RcyViewHolder h = new RcyViewHolder(cv);
			h.ll = (LinearLayout) cv.findViewById(R.id.ll);
			h.iv = (ImageView) cv.findViewById(R.id.iv);
			h.pb = (ProgressBar) cv.findViewById(R.id.pb);
			h.tv[0] = (TextView) cv.findViewById(R.id.tv0);
			h.tv[1] = (TextView) cv.findViewById(R.id.tv1);
			h.tv[2] = (TextView) cv.findViewById(R.id.tv2);
			h.tv[3] = (TextView) cv.findViewById(R.id.tv3);
			h.bt_del = (Button) cv.findViewById(R.id.bt_del);
			h.ll_progress = (LinearLayout) cv.findViewById(R.id.ll_progress);
			h.bt = (Button) cv.findViewById(R.id.bt);
			return h;
		}
	}

	List<Object> rcylist;
	RcyListTask rcylistTask;

	public void refeshRcyList() {
		if (rcylistTask != null) {
			rcylistTask.cancel(true);
		}
		rcylistTask = new RcyListTask(this);
		rcylistTask.execute();
	}

	private class RcyListTask extends ExceptionAsyncTask<List<Object>> {

		public RcyListTask(Context context) {
			super(context);
		}

		@Override
		public List<Object> doInBackgroundX(String... params) throws Exception {
			return null;
		}

		@Override
		public void onPostExecuteX(List<Object> result) {
			if (result == null) {
				return;
			} else {
				rcylist = result;
				rcy_adp.notifyDataSetChanged();
			}
		}
	}

	List<DownloadCourseInfo>list;
	private void loadData() {
		DBUtil db = new DBUtil(getActivity());
		list = db.findAll(DownloadCourseInfo.class);
		rcy_adp.notifyDataSetChanged();
	}

	@Override
	public void onDestroy() {
		Cleaner.cleanTask(rcylistTask);
		super.onDestroy();
	}

}